package ${sourceBasePackage}.service;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.bean.copier.CopyOptions;
import cn.hutool.core.date.DateUtil;
import com.bidb.core.context.ContextUtil;
import com.bidb.core.dao.BaseEntityDao;
import com.bidb.core.dto.serach.PageInfo;
import com.bidb.core.dto.serach.PageResult;
import com.bidb.core.dto.serach.Search;
import com.bidb.core.dto.serach.SearchFilter;
import com.bidb.core.service.BaseEntityService;
import com.bidb.util.DateUtils;
import com.bidb.util.IdGenerator;
import com.cscec.smart.site.safety.utils.ProjectUtil;
import org.apache.commons.lang3.ObjectUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import ${sourceBasePackage}.entity.${entityName};
import ${sourceBasePackage}.dto.${entityName}Dto;
import ${sourceBasePackage}.dto.${entityName}SearchDto;
import ${sourceBasePackage}.dao.${entityName}Dao;

import java.util.List;

/**
 * ${description}(${entityName})业务逻辑实现类
 *
 * @author ${author}
 * @since ${createDate}
 */
@Service
public class ${entityName}Service extends BaseEntityService<${entityName}> {

	@Autowired
	private ${entityName}Dao dao;

    @Override
	protected BaseEntityDao<${entityName}> getDao() {
	    return dao;
	}

	public PageResult<${entityName}> findPage(${entityName}SearchDto ${entityName?uncap_first}SearchDto) {

		Search search = new Search();

        PageInfo pageInfo = new PageInfo();
        pageInfo.setPage(${entityName?uncap_first}SearchDto.getPage());
        pageInfo.setRows(${entityName?uncap_first}SearchDto.getRows());
        search.setPageInfo(pageInfo);

        search.addFilter(new SearchFilter("tyProjectCode", ProjectUtil.getProjectInfo().getProjectCode()));

        <#list searchFields as po>
        <#if po.propertyName != 'tyProjectCode'>
        <#if po.listQuery == 'interval'>
        if(ObjectUtils.isNotEmpty(${entityName?uncap_first}SearchDto.get${po.propertyName?cap_first}Start())) {
            search.addFilter(new SearchFilter("${po.propertyName}", ${entityName?uncap_first}SearchDto.get${po.propertyName?cap_first}Start(), SearchFilter.Operator.GE));
        }
        if(ObjectUtils.isNotEmpty(${entityName?uncap_first}SearchDto.get${po.propertyName?cap_first}End())) {
            <#if po.propertyType == 'Date'>
            ${entityName?uncap_first}SearchDto.set${po.propertyName?cap_first}End(DateUtil.endOfDay(${entityName?uncap_first}SearchDto.get${po.propertyName?cap_first}End()));
            </#if>
            search.addFilter(new SearchFilter("${po.propertyName}", ${entityName?uncap_first}SearchDto.get${po.propertyName?cap_first}End(), SearchFilter.Operator.LE));
        }
        <#else>
        if(ObjectUtils.isNotEmpty(${entityName?uncap_first}SearchDto.get${po.propertyName?cap_first}())) {
            search.addFilter(new SearchFilter("${po.propertyName}", ${entityName?uncap_first}SearchDto.get${po.propertyName?cap_first}()));
        }
        </#if>
        </#if>
        </#list>

		PageResult<${entityName}> pageResult = dao.findByPage(search);

		return pageResult;
	}

	public List<${entityName}> findList(${entityName}SearchDto ${entityName?uncap_first}SearchDto) {

        Search search = new Search();

        search.addFilter(new SearchFilter("tyProjectCode", ProjectUtil.getProjectInfo().getProjectCode()));

        <#list searchFields as po>
            <#if po.propertyName != 'tyProjectCode'>
                <#if po.listQuery == 'interval'>
        if(ObjectUtils.isNotEmpty(${entityName?uncap_first}SearchDto.get${po.propertyName?cap_first}Start())) {
            search.addFilter(new SearchFilter("${po.propertyName}", ${entityName?uncap_first}SearchDto.get${po.propertyName?cap_first}Start(), SearchFilter.Operator.GE));
        }
        if(ObjectUtils.isNotEmpty(${entityName?uncap_first}SearchDto.get${po.propertyName?cap_first}End())) {
                    <#if po.propertyType == 'Date'>
            ${entityName?uncap_first}SearchDto.set${po.propertyName?cap_first}End(DateUtil.endOfDay(${entityName?uncap_first}SearchDto.get${po.propertyName?cap_first}End()));
                    </#if>
            search.addFilter(new SearchFilter("${po.propertyName}", ${entityName?uncap_first}SearchDto.get${po.propertyName?cap_first}End(), SearchFilter.Operator.LE));
        }
                <#else>
        if(ObjectUtils.isNotEmpty(${entityName?uncap_first}SearchDto.get${po.propertyName?cap_first}())) {
            search.addFilter(new SearchFilter("${po.propertyName}", ${entityName?uncap_first}SearchDto.get${po.propertyName?cap_first}()));
        }
                </#if>
            </#if>
        </#list>

		List<${entityName}> list = dao.findByFilters(search);

		return list;
	}

    public void add(${entityName}Dto ${entityName?uncap_first}Dto) {

        ${entityName} ${entityName?uncap_first} = new ${entityName}();
        BeanUtil.copyProperties(${entityName?uncap_first}Dto, ${entityName?uncap_first});
        ${entityName?uncap_first}.set${pkPropertyName?cap_first}(IdGenerator.uuid());
        ${entityName?uncap_first}.setTyCreateTime(DateUtils.getCurrentDateTime());
        ${entityName?uncap_first}.setTyCreateUserid(ContextUtil.getUserId());
        ${entityName?uncap_first}.setTyCreateUserName(ContextUtil.getUserName());
        ${entityName?uncap_first}.setTyProjectCode(ProjectUtil.getProjectInfo().getProjectCode());

        dao.save(${entityName?uncap_first});
    }

    public void update(${entityName}Dto ${entityName?uncap_first}Dto) {

        ${entityName} ${entityName?uncap_first}Old = dao.findOne(${entityName?uncap_first}Dto.get${pkPropertyName?cap_first}());

        if(${entityName?uncap_first}Old != null){

            CopyOptions copyOptions = CopyOptions.create();
            copyOptions.setIgnoreNullValue(true);
            BeanUtil.copyProperties(${entityName?uncap_first}Dto, ${entityName?uncap_first}Old, copyOptions);
            ${entityName?uncap_first}Old.setTyLastUpdateTime(DateUtils.getCurrentDateTime());
            ${entityName?uncap_first}Old.setTyLastUpdateUserid(ContextUtil.getUserId());
            ${entityName?uncap_first}Old.setTyLastUpdateUserName(ContextUtil.getUserName());

           dao.save(${entityName?uncap_first}Old);
        }
    }

    public void deleteById(${pkPropertyType} ${pkPropertyName}) {

        ${entityName} ${entityName?uncap_first}Old = dao.findOne(${pkPropertyName});

        if(${entityName?uncap_first}Old != null){

            // ${entityName?uncap_first}Old.setTyDeleteStatus(1);
            ${entityName?uncap_first}Old.setTyLastUpdateTime(DateUtils.getCurrentDateTime());
            ${entityName?uncap_first}Old.setTyLastUpdateUserid(ContextUtil.getUserId());
            ${entityName?uncap_first}Old.setTyLastUpdateUserName(ContextUtil.getUserName());

            dao.save(${entityName?uncap_first}Old);
        }
    }

}
